package com.zyw.zhuangdian.controller;

import com.alibaba.fastjson.JSONObject;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.dto.PageDto;
import com.zyw.zhuangdian.pojo.in.FavouriteDtoIn;
import com.zyw.zhuangdian.service.FavouriteService;
import com.zyw.zhuangdian.util.Assert;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import com.zyw.zhuangdian.util.RespUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/favourite")
@RequiredArgsConstructor
@Slf4j
public class FavouriteController {
    private final FavouriteService favouriteService;

    /**
     * 新增收藏
     * @param json
     * @return
     */
    @RequestMapping(value = "doFavourite",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> addFavourite(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"fromUid","收藏者id不能为空");
        Assert.hasKeyAndValue(json,"toAid","收藏内容id不能为空");
        Assert.hasKeyAndValue(json,"type","收藏类型不能为空");

        FavouriteDtoIn in = BeanConvertUtil.covertBean(json, FavouriteDtoIn.class);
        Resp<Void> resp = favouriteService.addFavourite(in);
        return RespUtil.OK(resp);
    }


    /**
     * 删除收藏
     * @param json aid
     * @return
     */
    @RequestMapping(value = "cancelFavourite",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> deleteFavourite(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"fromUid","收藏者id不能为空");
        Assert.hasKeyAndValue(json,"toAid","收藏内容id不能为空");
        Assert.hasKeyAndValue(json,"type","收藏类型不能为空");

        FavouriteDtoIn in = BeanConvertUtil.covertBean(json, FavouriteDtoIn.class);
        Resp<Void> resp = favouriteService.deleteFavourite(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出收藏
     * @param json
     * @return
     */
    @RequestMapping(value = "listFavourite",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listFavourite(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"fromUid","收藏者id不能为空");
        Assert.hasKeyAndValue(json,"type","收藏类型不能为空"); // production or article

        FavouriteDtoIn in = BeanConvertUtil.covertBean(json, FavouriteDtoIn.class);
        Resp<List<? extends PageDto>> resp = favouriteService.listFavourites(in);
        return RespUtil.OK(resp);
    }
}
